var path = require('path');
var webpack = require('webpack');
var HtmlWebpackPlugin = require('html-webpack-plugin');
var StatsPlugin = require('stats-webpack-plugin');
var ExtractTextPlugin = require('extract-text-webpack-plugin');
var TransferWebpackPlugin = require('transfer-webpack-plugin');
var node_modules_dir = __dirname + '/node_modules';
const isDeveloping = process.env.NODE_ENV === 'production';
var publicPath = isDeveloping === true ? '//i0.yztcdn.com/app_js/h5/finance/insureRevision/'
	: '//i0stg.yztcdn.com/app_js/h5/finance/insureRevision/'
var config = {
	cache: true,
	entry: {
		app: path.resolve(__dirname, 'src/index.js'),
		shared: [
			'react',
			'react-router',
			'react-redux',
			'redux',
			//'lodash/core',

			'babel-polyfill',
			'actions',
			'reqwest',
			'components/modal'
		]
	},
	output: {
		path: path.join(__dirname, '/dist/'),
		filename: publicPath+'js/app.js',
		chunkFilename: publicPath+'js/chunk.[id].[hash:4].js',
		//cdn host
		//publicPath: 'http://script2.pingan.com/app_js/c3/chaoshi/helium/'
		//publicPath:'/chaoshi/pages/hqlc/'
		publicPath:''
	},
	resolve: {
		modulesDirectories: [
			'src',
			'node_modules',
      'src/assets'
		],
		extensions: ['', '.json', '.js', '.png'],
		alias: {
			'react': 'react-lite',
			'react-dom': 'react-lite'
		}
	},
	module: {
		loaders: [{
			test: /\.less$/,
			loader: ExtractTextPlugin.extract(
				'css?-minimize!' + 'autoprefixer-loader!' + 'less'
			)
		}, {
			test: /\.(js|jsx)?$/,
			exclude: /node_modules/,
			loaders: ['babel?optional=runtime&stage=0']
		}, {
			test: /\.json?$/,
			loader: 'json'
		}, {
			test: /\.css$/,
			loader: ExtractTextPlugin.extract('style', 'css!postcss')
		}, {
			test: /\.(jp?g|gif|png|woff|ico)$/,
			loaders: ['url-loader?limit=8192&name=[name].[hash:4].[ext]', 'img?{bypassOnDebug: true, progressive:true, optimizationLevel: 3, pngquant:{quality: "65-80"}}']
		}]
	},
	imagemin: {
		gifsicle: {
			interlaced: false
		},
		jpegtran: {
			progressive: true,
			arithmetic: false
		},
		optipng: {
			optimizationLevel: 5
		},
		pngquant: {
			floyd: 0.5,
			speed: 2
		},
		svgo: {
			plugins: [{
				removeTitle: true
			}, {
				convertPathData: false
			}]
		}
	},
	plugins: [
		new HtmlWebpackPlugin({
			template: 'src/index.tpl.html',
			inject: 'body',
			filename: 'index.html'
		}),
		new StatsPlugin('webpack.stats.json', {
			source: false,
			modules: true
		}),
		new ExtractTextPlugin(publicPath+'css/app.css'),
		new webpack.optimize.CommonsChunkPlugin('shared', publicPath+'js/shared.js'),
		new TransferWebpackPlugin([
			{ from: 'assets/images', to: publicPath+'images/'}
		], path.join(__dirname, 'src')),
		new webpack.optimize.DedupePlugin(),
		// new webpack.optimize.UglifyJsPlugin({
		// 	sourceMap: false,
		// 	cache: false,
		// 	compressor: {
		// 		warnings: false,
		// 		screw_ie8: false
		// 	},
		// 	output: {
		// 		comments: false
		// 	}
		// }),
		new webpack.optimize.OccurenceOrderPlugin(),
		new webpack.optimize.AggressiveMergingPlugin({
			minSizeReduce: 1.5,
			moveToParents: true
		}),
		new webpack.DefinePlugin({
			'process.env.NODE_ENV': JSON.stringify(process.env.NODE_ENV)
		}),
	],
	postcss: [
		require('autoprefixer')
	]
};
if(!isDeveloping){
	config.plugins.push(new webpack.optimize.UglifyJsPlugin({
		sourceMap: false,
		cache: false,
		compressor: {
			warnings: false,
			screw_ie8: false
		},
		output: {
			comments: false
		}
	}))
}
module.exports = config;
